﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMS_Master.Dto;
using TMS_Master.Entities;
using TMS_Master.Interface;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace TMS_Master.DomainService
{
    public class Goods_ReceivingService: ApplicationService, IGoods_ReceivingService
    {
        #region 注入仓储接口
        private readonly IRepository<Goods_Receiving, Guid> _goodsRepository; //物资领用

        public Goods_ReceivingService(IRepository<Goods_Receiving, Guid> goodsRepository)
        {
            _goodsRepository = goodsRepository;
        }
        #endregion

        #region 修改
        [HttpPost]
        public async Task<Goods_ReceivingDto> EditGoods(Goods_ReceivingUpdateDto Cg)
        {
            try
            {
                var res = ObjectMapper.Map<Goods_ReceivingUpdateDto, Goods_Receiving>(Cg);
                var result = await _goodsRepository.UpdateAsync(res);
                return ObjectMapper.Map<Goods_Receiving, Goods_ReceivingDto>(result);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 反填
        [HttpGet]
        public async Task<Goods_ReceivingDto> FullGoods(Guid Id)
        {
            try
            {
                var query = await _goodsRepository.GetAsync(Id);
                return ObjectMapper.Map<Goods_Receiving, Goods_ReceivingDto>(query);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 删除
        [HttpPost]
        public async Task<int> DelCargo(Guid Id)
        {
            try
            {
                await _goodsRepository.DeleteAsync(Id);
                return 1;
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 详情
        [HttpGet]
        public async Task<Goods_ReceivingDto> Details(Guid Id)
        {
            try
            {
                var query = await _goodsRepository.GetAsync(Id);
                return ObjectMapper.Map<Goods_Receiving, Goods_ReceivingDto>(query);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 提交
        [HttpPost]
        public Task<Goods_ReceivingDto> ProvRefer(Guid Id)
        {
            try
            {
                var res = _goodsRepository.FindAsync(Id);
                res.Result.IsState = (TmsEnum.Status)2;
                return (Task<Goods_ReceivingDto>)UnitOfWorkManager.Current.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 重新添加
        [HttpPost]
        public Task<Goods_ReceivingDto> ProvAnew(Guid Id)
        {
            try
            {
                var res = _goodsRepository.FindAsync(Id);
                res.Result.IsState = (TmsEnum.Status)2;
                return (Task<Goods_ReceivingDto>)UnitOfWorkManager.Current.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 显示
        [HttpGet]
        public async Task<PagedResultDto<Goods_ReceivingDto>> GetList(int PageIndex=1, int PageSize=4, string name="", string date = "", string state = "", string uname = "")
        {
            try
            {
                var linq = await _goodsRepository.GetListAsync();

                if (!string.IsNullOrEmpty(name))
                {
                    linq = linq.Where(s => s.GoodsTitle.Contains(name)).ToList();
                }
                if (!string.IsNullOrEmpty(date))
                {
                    linq = linq.Where(s => s.RcTime.Equals(date)).ToList();
                }
                if (!string.IsNullOrEmpty(state))
                {
                    linq = linq.Where(s => s.IsState.Equals(state)).ToList();
                }
                if (!string.IsNullOrEmpty(uname))
                {
                    linq = linq.Where(s => s.Approver.Contains(uname)).ToList();
                }
                //总条数
                int TotalCount = linq.Count;

                var result = linq.Skip((PageIndex - 1) * PageSize).Take(PageSize);

                var res = ObjectMapper.Map<List<Goods_Receiving>, List<Goods_ReceivingDto>>(result.ToList());

                return new PagedResultDto<Goods_ReceivingDto>(TotalCount, res);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 添加
        [HttpPost]
        public async Task<int> CreateGoods(Goods_ReceivingUpdateDto Cg)
        {
            try
            {
                var query = ObjectMapper.Map<Goods_ReceivingUpdateDto, Goods_Receiving>(Cg);
                await _goodsRepository.InsertAsync(query);
                return 1;
            }
            catch (Exception)
            {

                throw;
            }
            

        }
        #endregion
    }
}
